/*
 * 课时
 */
import Lesson from 'constants/lesson';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 查询课时
 * @param lessonId 课时ID
 */
export function fetchLessonDetail(courseId: number, lessonId: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.FETCH, Lesson.FETCH_SUCCESS, Lesson.FETCH_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons/${lessonId}`,
      method: 'GET',
    },
  };
}

/**
 * 创建课时
 * @param courseId 课程ID
 * @param data 课时模型LessonModal
 */
export function createLesson(courseId: number, data: Record<string, any>) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [Lesson.CREATE, Lesson.CREATE_SUCCESS, Lesson.CREATE_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons`,
      method: 'POST',
      body: data,
    },
  };
}

export function updateLesson(courseId: number, lessonId: number, data: any) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.UPDATE, Lesson.UPDATE_SUCCESS, Lesson.UPDATE_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons/${lessonId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 删除Lesson
 */
export function removeLesson(courseId: number, lessonId: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.UPDATE, Lesson.UPDATE_SUCCESS, Lesson.UPDATE_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons/${lessonId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 更新课时发布状态
 * @param lessonId
 * @param status: 状态： 0: 未发布，1: 已发布，2: pending状态（课时已发布后做出修改，调用此接口置状态为pending，发布按钮变化提示重新发布）
 */
export function updateLessonPublish(lessonId: number, status: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [
        Lesson.UPDATE_PUBLISH_STATUS,
        Lesson.UPDATE_PUBLISH_STATUS_SUCCESS,
        Lesson.UPDATE_PUBLISH_STATUS_FAIL,
      ],
      uri: `/course-api/a/lessons/${lessonId}/publish`,
      method: 'PUT',
      body: {
        status,
      },
    },
  };
}

/**
 * 查询课程ID的所有课时
 * @param courseId 课程ID
 */
export function fetchCourseLessons(courseId: number) {
  return {
    courseId,
    [CALL_BOOK_API]: {
      types: [Lesson.FETCH_LIST, Lesson.FETCH_LIST_SUCCESS, Lesson.FETCH_LIST_FAIL],
      uri: `/course-api/a/courses/${courseId}/lessons`,
      method: 'GET',
    },
  };
}

/**
 * 更新课时封面图片
 * @param lessonId
 * @param imageId
 * @param type: 0: 原图(封面), 1: 缩略图
 */
export function saveLessonCover(lessonId: number, imageId: number, type: number) {
  let body: any = { type };
  if (imageId) {
    body = { ...body, imageId };
  }
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.SAVE_COVER, Lesson.SAVE_COVER_SUCCESS, Lesson.SAVE_COVER_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/cover`,
      method: 'PUT',
      body,
    },
  };
}

/**
 * 更新音频(开场白)
 * @param audio 音频
 */
export function saveLessonAudio(lessonId: number, audioId: number) {
  let body = {};
  if (audioId) {
    body = { audioId };
  }
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.SAVE_AUDIO, Lesson.SAVE_AUDIO_SUCCESS, Lesson.SAVE_AUDIO_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/audio`,
      method: 'PUT',
      body,
    },
  };
}

/**
 * 绘本内容|精读排序
 * @param lessonId
 * @param pageIds
 */
export function sortLessonPages(lessonId: number, pageIds: number[]) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.SORT_PAGE, Lesson.SORT_PAGE_SUCCESS, Lesson.SORT_PAGE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages-sequence`,
      method: 'PUT',
      body: {
        pageIds,
      },
    },
  };
}

/**
 * 闯关测试排序
 * @param lessonId
 * @param testIds
 */
export function sortLessonTests(lessonId: number, testIds: number[]) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Lesson.SORT_TEST, Lesson.SORT_TEST_SUCCESS, Lesson.SORT_TEST_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/tests-sequence`,
      method: 'PUT',
      body: {
        testIds,
      },
    },
  };
}

/**
 * 音频识别
 * @param uri 音频uri
 * @param text 音频脚本
 */
export function recognitionAudio(uri: string, text: string) {
  return {
    uri,
    [CALL_BOOK_API]: {
      types: [
        Lesson.RECOGNITION_AUDIO,
        Lesson.RECOGNITION_AUDIO_SUCCESS,
        Lesson.RECOGNITION_AUDIO_FAIL,
      ],
      uri: `/service-api/audio/word-time`,
      method: 'POST',
      body: {
        uri,
        text,
      },
    },
  };
}
